﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Extensil.Common.CategoryHandler.Discovery;
using Extensil.Common.CategoryHandler;
using Extensil.Processor.Extensibility;

namespace Extensil.Processor
{
    public class DiscoveryManager
    {
        public delegate void ReportProgress(int progress, string message);
        ReportProgress _rp;
        public DiscoveryManager(ReportProgress rp)
        {
            _rp = rp;
        }
        public void ReportStatus(string message)
        {
            _rp(10, message);
        }
        public static bool IsDiscoveryEnabled
        {
            get
            {
                if(CategorySection.GetConfig().DiscoverySection != null)
                    return CategorySection.GetConfig().DiscoverySection.IsActive;
                return false;
            }
        }
        public DiscoveryResultList Discover()
        {
            DiscoveryResultList result = new DiscoveryResultList();
            try
            {
                DiscoverySection ds = CategorySection.GetConfig().DiscoverySection;
                if(!ds.IsActive)
                {
                    ReportStatus("Please switch on the auto-upgradation mode to enable automatic upgrade.");
                    return null;
                }
                for(int i = 0; i < ds.Count; i++)
                {
                    try
                    {
                        Probe prb = ds[i];
                        if(!prb.IsActive)
                        {
                            ReportStatus("Skipping discovery for probe path " + prb.Name + " as it is switch off.");
                            continue;
                        }
                        if(CategorySection.GetConfig().ProbeHandlers[prb.Handler] == null)
                            throw new Exception("No handler configured to handle '"+prb.Handler+"' type discovery path.");
                        string prbHandler = CategorySection.GetConfig().ProbeHandlers[prb.Handler].Path;
                        BaseProbeHandler cd = PluginAssembly.GetHandler<BaseProbeHandler>(prbHandler);
                        cd.SetReportEvent(ReportStatus);
                        ProbeParamList paramList = new ProbeParamList();
                        for(int j = 0; j < prb.Count; j++)
                        {
                            paramList.Add(prb[j]);
                        }
                        cd.ConnectProbe(paramList);
                        ReportStatus("Connected to probe path " + prb.Name + ".");
                        result.AddRange(cd.RunDiscovery());
                        ReportStatus("Discovery completed at probe path " + prb.Name + ".");
                    }
                    catch(Exception ex)
                    {
                        ReportStatus("Error: " + ex.Message);
                    }
                }
                ReportStatus("Discovery completed.");
            }
            catch(Exception exc)
            {
                ReportStatus("Error: " + exc.Message);
            }
            return result;
        }
    }
}
